package com.chenggong.retrofit;

import android.util.Log;

import com.chenggong.retrofit.bean.GithubBean;
import com.chenggong.retrofit.bean.QqBean;
import com.chenggong.retrofit.retrofit.ApiService;
import com.chenggong.retrofit.retrofit.ApiServiceManager;
import com.chenggong.retrofit.rxjava.ApiRxjavaService;
import com.chenggong.retrofit.rxjava.ApiRxjavaServiceManager;

import java.io.IOException;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 应用例子
 */
public enum TestTool {
    INSTANCE;
    public void getGithubData() {
        //发送异步请求
        //创建网络请求接口的实例
        ApiService apiService = ApiServiceManager.get().create(ApiService.class);
        //对发送请求进行封装
        Call<GithubBean> call = apiService.getGithubData("hongyangAndroid");
        call.enqueue(new Callback<GithubBean>() {
            @Override
            public void onResponse(Call<GithubBean> call, Response<GithubBean> response) {
                //网络请求成功
                if (response.body() == null) {
                    return;
                }
                GithubBean githubBean = response.body();
                Log.e("TestTool", githubBean.getLogin());
            }

            @Override
            public void onFailure(Call<GithubBean> call, Throwable t) {
                //网络请求失败
                Log.e("TestTool", t.getMessage());
            }
        });
    }

    public void getQqDataSync() {
        //发送同步请求,需要在子线程中执行
        ApiService apiService = ApiServiceManager.get().create(ApiService.class);
        Call<QqBean> call = apiService.getQqData("774740085");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response<QqBean> response = call.execute();//同步请求网络
                    if (response.body() == null) {
                        Log.e("TestTool", "无数据");
                        return;
                    }
                    QqBean qqBean = response.body();
                    Log.e("TestTool", "name=" + qqBean.name);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void getRxjavaGithubData() {
        //发送异步请求
        //创建网络请求接口的实例
        ApiRxjavaService apiService = ApiRxjavaServiceManager.get().create(ApiRxjavaService.class);
        //retrofit2+rxjava2
        apiService.getGithubData("hongyangAndroid")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GithubBean>(){

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GithubBean value) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
